package DataObjects;

import java.awt.Component;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ButtonPointOfSale extends JButton{
	
	private String name;
	

	public  ButtonPointOfSale(String name)
	{
		super();
		this.name=name;
		initialize();
		
		
	}
	
	private void initialize()
	{
		this.setText(name);
		//this.addActionListener();
		this.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				
				String actionCommand = e.getActionCommand();
		        Component component=KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
		        JTextField  txtF=(JTextField)component;
		       if(txtF.getText().isEmpty())
		       {
		    	   String text= name;
		    	   txtF.setText(text);
		       }
		       else
		       {
		       String text=Integer.toString(Integer.parseInt(txtF.getText())+Integer.parseInt(name));
		       txtF.setText(text);
		       }
		        int caret=txtF.getCaretPosition();
		       
		                   
		    //    txtF.setText(text.substring(0,caret) +actionCommand+ text.substring(caret));
		        txtF.setCaretPosition(caret+txtF.getText().length());	
				
			}
		});
	}
	
	
	/* public void actionPerformed(ActionEvent e)
	    {
	    	String actionCommand = e.getActionCommand();
	        Component component=KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
	        JTextField  txtF=(JTextField)component;
	        int caret=txtF.getCaretPosition();
	        String text=txtF.getText();
	        
	                   
	        txtF.setText(text.substring(0,caret) +actionCommand+ text.substring(caret));
	        txtF.setCaretPosition(caret+txtF.getText().length());
	    }*/
}
